package com.chunxiadong.service.impl;

import com.chunxiadong.entity.Avatar;
import com.chunxiadong.entity.Picture;
import com.chunxiadong.mapper.PictureMapper;
import com.chunxiadong.service.PictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/8
 */
@Service
public class PictureServiceImpl implements PictureService {
    @Autowired
    PictureMapper pictureMapper;

    @Transactional
    @Override
    public int addPicture(Picture picture) {
        picture.setPictureTime(new Date());
        return pictureMapper.addPicture(picture);
    }

    @Transactional
    @Override
    public int updatePicture(Picture picture) {
        return pictureMapper.updatePicture(picture);
    }

    @Transactional
    @Override
    public List<Picture> getAllPicture() {
        return pictureMapper.getAllPicture();
    }

    @Transactional
    @Override
    public PageInfo<Picture> getAllPicturePageInfo(Integer pageNum, Integer rows) {
        //按照排序字段 倒序 排序
        String orderBy = "pictureTime desc";
        PageHelper.startPage(pageNum, rows, orderBy);
        List<Picture> allPicture = pictureMapper.getAllPicture();
        PageInfo<Picture> pageInfo = new PageInfo<>(allPicture);
        return pageInfo;
    }

    @Transactional
    @Override
    public int deletePicture(Integer id) {
        return pictureMapper.deletePicture(id);
    }

    @Transactional
    @Override
    public Picture queryPictureByPicAddr(String picAddr) {
        return pictureMapper.queryPictureByPicAddr(picAddr);
    }

    @Transactional
    @Override
    public int setAvatar(Avatar avatar) {
        return pictureMapper.setAvatar(avatar);
    }

    @Transactional
    @Override
    public Picture queryPictureById(Integer id) {
        return pictureMapper.queryPictureById(id);
    }
}
